import type { XtxGuessInstance } from '@/components/component'
import { ref, watch } from 'vue'

/**
 * 猜你喜欢的组合式函数
 * @returns 返回 guessRef 和 onScrolltolower
 */
export const useGuessList = () => {
  // 获取猜你喜欢组件实例
  const guessRef = ref<XtxGuessInstance>()

  // 滚动触底事件
  const onScrolltolower = () => {
    guessRef.value?.getMore()
  }

  const scrollY = ref(true)
  // 侦听请求变化
  watch(
    () => guessRef.value?.isSending,
    (state) => {
      scrollY.value = !state!
      if (state) {
        uni.showLoading({ title: '加载中~', mask: true })
      } else {
        uni.hideLoading()
      }
    },
  )

  // 返回 ref 和事件处理函数
  return {
    scrollY,
    guessRef,
    onScrolltolower,
  }
}
